/* eslint-disable */
import * as XLSX from "XLSX";

interface excelType {
  json: object;
  name: string;
  titleArr: string[];
  sheetName: string;
}

export function exportExcel(params: excelType) {
  const keyArray: any = [];
  let data: any = [];
  const getLength = function (obj: object) {
    let count = 0;
    for (const i in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, i)) {
        count++;
      }
    }
    return count;
  };

  for (const key1 in params.json) {
    if (Object.prototype.hasOwnProperty.call(params.json, key1)) {
      const element = (params.json as { [key: string]: object })[key1];
      const rowDataArray: any = [];
      for (const key2 in element) {
        if (Object.prototype.hasOwnProperty.call(element, key2)) {
          const element2 = (element as { [key: string]: object })[key2];
          rowDataArray.push(element2);
          if (keyArray.length < getLength(element)) {
            keyArray.push(key2);
          }
        }
      }
      data.push(rowDataArray);
    }
  }
  data.splice(0, 0, keyArray as any, params.titleArr as any);
  console.log(data);
  let ws = XLSX.utils.aoa_to_sheet(data);
  let wb = XLSX.utils.book_new();
  // 隐藏英文字段表头
  const wsrows = [{ hidden: true }];
  /* 设置worksheet每列的最大宽度 */
  const colWidth = data.map(row =>
    row.map(val => {
      /* 先判断是否为null/undefined */
      if (val == null) {
        return {
          wch: 10
        };
      } else if (val.toString().charCodeAt(0) > 255) {
        /* 再判断是否为中文 */
        return {
          wch: val.toString().length * 2.1
        };
      } else {
        return {
          wch: val.toString().length * 1.05
        };
      }
    })
  );
  /* 以第一行为初始值 */
  const result = colWidth[0];
  for (let i = 1; i < colWidth.length; i++) {
    for (let j = 0; j < colWidth[i].length; j++) {
      if (result[j].wch < colWidth[i][j].wch) {
        result[j].wch = colWidth[i][j].wch;
      }
    }
  }
  ws["!cols"] = result;
  ws["!rows"] = wsrows; // ws - worksheet
  XLSX.utils.book_append_sheet(wb, ws, params.sheetName);
  /* generate file and send to client */
  XLSX.writeFile(wb, `${params.name}.xlsx`);
}

// export default {
//   export_json_to_excel(th, jsonData, defaultTitle) {
//     /* original data */

//     var data = jsonData;
//     data.unshift(th);
//     var ws_name = "SheetJS";

//     var wb = new Workbook(),
//       ws = sheet_from_array_of_arrays(data);

//     // /*设置worksheet每列的最大宽度*/
//     const colWidth = data.map(row =>
//       row.map(val => {
//         /*先判断是否为null/undefined*/
//         if (val == null) {
//           return { wch: 10 };
//         } else if (val.toString().charCodeAt(0) > 255) {
//           /*再判断是否为中文*/
//           return { wch: val.toString().length * 2 };
//         } else {
//           return { wch: val.toString().length };
//         }
//       })
//     );
//     /*以第一行为初始值*/
//     let result = colWidth[0];
//     for (let i = 1; i < colWidth.length; i++) {
//       for (let j = 0; j < colWidth[i].length; j++) {
//         if (result[j]["wch"] < colWidth[i][j]["wch"]) {
//           result[j]["wch"] = colWidth[i][j]["wch"];
//         }
//       }
//     }
//     ws["!cols"] = result;

//     /* add worksheet to workbook */
//     wb.SheetNames.push(ws_name);
//     wb.Sheets[ws_name] = ws;

//     var wbout = XLSX.write(wb, { bookType: "xlsx", bookSST: false, type: "binary" });
//     var title = defaultTitle || "excel-list";
//     saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), title + ".xlsx");
//   }
// };
